package m.jclecai.com.adapter;

import android.content.Context;
import android.view.ViewGroup;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import java.util.Map;

import m.jclecai.com.R;
import m.jclecai.com.adapter.base.ViewHolerAdapter;
import m.jclecai.com.adapter.holder.HotTrendHolder;

/**
 * Created by ZD on 2017/4/27.
 */

public class HotTrendAdapter extends ViewHolerAdapter<HotTrendHolder> {

    Context mContext;
    JsonArray datas;

    public HotTrendAdapter(Context context, JsonArray datas) {
        this.mContext = context;
        this.datas = datas;

    }

    @Override
    public HotTrendHolder onCreateViewHolder(int position, ViewGroup parent) {
        return new HotTrendHolder(mContext, R.layout.view_hot_trend);
    }

    @Override
    public void onBindViewHolder(int position, HotTrendHolder hotTrendHolder) {
        JsonObject jb = datas.get(position).getAsJsonObject();
        for (Map.Entry<String, JsonElement> entry:jb.entrySet()){
            if(entry.getKey().contains("num_info"))
                hotTrendHolder.tv_num.setText(entry.getValue().getAsString());
            if(entry.getKey().contains("countid"))
                hotTrendHolder.tv_count.setText(entry.getValue().getAsString());
            if(entry.getKey().contains("countunm")) {
                hotTrendHolder.tv_bfb.setText(String.format(mContext.getString(R.string.sc_trend_bfb),entry.getValue().getAsFloat())+"%");
                hotTrendHolder.progress.setMax(10000);
                hotTrendHolder.progress.setProgress((int)entry.getValue().getAsFloat()*100);
            }
        }
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    public void updateNotifyDataSetChanged(JsonArray datas){
        this.datas = datas;
        notifyDataSetChanged();
    }
}
